import Taro, { Component } from '@tarojs/taro';
import { View, Text} from '@tarojs/components';
import http from '@/utils/http';
import utils from '@/utils/util';
import './index.scss'
import MedicinecabinetItem from '@/components/medicinecabinetItem/index';
import StatusPages from '@/components/statusPages/index';
import MoreFooter from '@/components/moreFooter/index';

let app = Taro.getApp();
class medicineCabinetManagement extends Component {
    
    constructor(props){
        super(props)
        this.state = {
            Medicalkits: [],
            moreType: 'none',
            statusType: 'none', // none 不显示空白页  empty //显示空白页
            statuserro: false, // 是否显示错误页
        }
    }
    config = {
        navigationBarTitleText: '药箱管理',
        enablePullDownRefresh: true
    }
    componentDidShow(){
        this.getMedicalkits();
    }
    onPullDownRefresh(){
        this.getMedicalkits();
    }
    getMedicalkits() { // 查询药箱
        let { Medicalkits} = this.state;
        let wardid = app.getValueSync('lastwardId');
        http.get('getMedicalkit', {
            wardid: wardid
        }).then((d) => {
            Taro.stopPullDownRefresh();
            d.datas.map((item)=>{
                delete item['medicalkits'];
            })
            this.setState({
                moreType: 'noMore',
                Medicalkits: d.datas,
                statusType: d.datas.length > 0 ? 'none' : 'empty',
                statuserro: false,
            })
        }).catch((e) => {
            Taro.stopPullDownRefresh();
            this.setState({
                moreType: Medicalkits.length > 0 ? 'error' : 'none',
                statusType: Medicalkits.length > 0 ? 'none' : 'empty',
                statuserro: Medicalkits.length > 0 ? false : true,
            })
            console.log(e)
        })
    }
    refresh() {
        this.getMedicalkits(); 
    }
    render() {
        let { Medicalkits, statuserro, statusType, moreType} = this.state;
        return (
            <View className='medical pt-2-5'>
                <View className='pl-3 pr-3 length-box ft28 ft_w600'>
                    <Text>急救药箱总计<Text style={{ color:'#FA5960'}}>{ Medicalkits.length }个</Text> </Text>
                </View>
                <StatusPages mt='mt-20'
                  refresh={() => { this.refresh() }}
                  statusType={statusType}
                  statuserro={statuserro}
                  title='当前病区未领取药箱'
                ></StatusPages>
                <View className='pl-2-5 pr-2-5'>
                    {
                        Medicalkits.map((item)=>{
                            return (
                                <View className='mt-2-5'
                                  key={item.id} 
                                  onClick={()=> { 
                                      Object.keys(item).map((items) => {
                                          item[items] = encodeURIComponent(item[items])
                                      })
                                      Taro.navigateTo({
                                            url: `/pages/medicalkitDetails/index?${utils.queryconcat(item)}`
                                        }) 
                                    }}
                                >
                                    <MedicinecabinetItem medicalkit={item}  ></MedicinecabinetItem>
                                </View>    
                            )
                        })
                    }
                </View>
                <MoreFooter moreType={moreType} restloading={() => { this.getMedicalkits() }} ></MoreFooter>
            </View>
        )
    }
}
export default medicineCabinetManagement